package kov.core.body.sb
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	
	public class SBXingZou extends BodyState
	{
		[Embed("../../../../../assets/sb/xingzou/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/sb/xingzou/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/sb/xingzou/2.gif")]
		private var Frame2:Class;
		[Embed("../../../../../assets/sb/xingzou/3.gif")]
		private var Frame3:Class;
		[Embed("../../../../../assets/sb/xingzou/4.gif")]
		private var Frame4:Class;
		[Embed("../../../../../assets/sb/xingzou/5.gif")]
		private var Frame5:Class;
		[Embed("../../../../../assets/sb/xingzou/6.gif")]
		private var Frame6:Class;
		[Embed("../../../../../assets/sb/xingzou/7.gif")]
		private var Frame7:Class;
		
		public function SBXingZou(body:IBody, priority:int=1)
		{
			super(StateEnum.XING_ZOU, body, priority);
			createRenderer();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			var f3:BitmapData = (new Frame3() as Bitmap).bitmapData;
			var f4:BitmapData = (new Frame4() as Bitmap).bitmapData;
			var f5:BitmapData = (new Frame5() as Bitmap).bitmapData;
			var f6:BitmapData = (new Frame6() as Bitmap).bitmapData;
			var f7:BitmapData = (new Frame7() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f3);
			this.renderer.push(f4);
			this.renderer.push(f5);
			this.renderer.push(f6);
			this.renderer.push(f7);
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 90;
			this.body.renderer.offsetX = 61;
			this.body.renderer.offsetY = 80;
			this.body.renderer.gotoAndPlay(0);
			
			Global.stage.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		override public function exit():void
		{
			Global.stage.removeEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(event:Event):void
		{
			var playerX:Number = Global.world.player.x;
			var playerY:Number = Global.world.player.y;
			var bodyX:Number = this.body.x;
			var bodyY:Number = this.body.y;
			var toX:Number = bodyX;
			var toY:Number = bodyY;
			if (bodyX - playerX > 100)
			{
				this.body.faceToLeft = true;
				toX = bodyX - 2;
			}
			else if (playerX - bodyX > 100)
			{
				this.body.faceToLeft = false;
				toX = bodyX + 2;
			}
			if ( !Global.world.mapData.blockData.isBlock(toX, toY) )
			{
				this.body.x = toX;
			}
			
			if (bodyY - playerY > 30)
			{
				toY = bodyY - 1;
			}
			else if (playerY - bodyY > 30)
			{
				toY = bodyY + 1;
			}
			if ( !Global.world.mapData.blockData.isBlock(toX, toY) )
			{
				this.body.y = toY;
			}
			
			// 检测是否已经靠近
			if ( Math.abs(playerX - this.body.x) <= 100 && Math.abs(playerY - this.body.y) <= 30 )
			{
				this.body.executeStateCommand(new StateCommand(StateEnum.BEI_ZHAN));
			}
		}
		
		
	}
}